const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property
    maxPos: number = 300;

    @property
    minPos: number= -200;

  
    birdNode: cc.Node = null

    up:cc.Node = null
    down:cc.Node = null
    concat:cc.Node = null

    isScore:boolean = false

  
    onLoad () {
        this.node.on("init",this.init,this)
        this.up = this.node.getChildByName("up")
        this.down = this.node.getChildByName("down")
    }

    start () {
        this.birdNode = cc.find("Canvas/player/bird")
    }

    init(e:cc.Event.EventCustom){
        let x = cc.winSize.width
        let y = Math.random()*(this.maxPos - this.minPos) + this.minPos
        this.up.y += y
        this.up.x = x

        this.down.y += y
        this.down.x = x
    }

    update (dt) {
        if(this.up.x < -cc.winSize.width){
            this.node.destroy()
            let layer = this.node.parent
            cc.log("柱子被销毁了，当前柱子有:" + layer.childrenCount)
        }

        if(!this.isScore && this.up.x < this.birdNode.x*2){
            this.isScore = true
            this.birdNode.emit("score")
        }
    }
}
